﻿// Copyright Microsoft

using System;
using System.Activities;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;

namespace Microsoft.Samples.SqlServer.Activities
{
  [Designer(typeof(Microsoft.Samples.SqlServer.Activities.Designers.LoginFormActivityDesigner))]
  public sealed class LoginFormActivity : CodeActivity
  {
    public InArgument<IntPtr> Handle {get; set;}
    public OutArgument<ICredentials> NetworkCredential { get; set; }
    private ICredentials _networkCredential;

    protected override void Execute(CodeActivityContext context)
    {    
        IntPtr handle = Handle.Get(context);

        LoginForm form = new LoginForm();
        form.DataAvailable += new LoginForm.ActivityFormEventHandler(form_DataAvailable);    

        form.ShowDialog(new MyWin32Window(handle));

        NetworkCredential.Set(context, _networkCredential);
    }

    //Get the users credentials
    void form_DataAvailable(object sender, ActivityFormEventArgs e)
    {
        _networkCredential = e.NetworkCredential;
    }
  }

  public class MyWin32Window : IWin32Window
  {
    private IntPtr _hwnd;

    public MyWin32Window(IntPtr handle)
    {
      _hwnd = handle;
    }

    public IntPtr Handle
    {
      get
      {
        return _hwnd;
      }
    }
  }
}
